package cn.com.darly.videoandroidpm.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.darly.common.ToastApp;
import com.darly.dview.common.SCfg;
import com.darly.dview.widget.camera.util.ImageLoaderUtil;

import java.util.List;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.bean.FragmentMainInfo;
import cn.com.darly.videoandroidpm.common.HTMLImageGetter;
import cn.com.darly.videoandroidpm.common.SystemLoginInfo;
import cn.com.darly.videoandroidpm.ui.LoginActivity;

/**
 * @author Darly/张宇辉/2018/3/14 16:11
 * @version 1.0/com.darly.darlyview.ui.adapter
 */

public class FragmentMainRecyclerAdapter extends RecyclerView.Adapter<FragmentMainRecyclerAdapter.ViewHocker> implements View.OnClickListener {
    private Context context;
    private List<FragmentMainInfo> list;
    private OnRecyclerItemClickListener listener;
    private RecyclerView recyclerView;

    public FragmentMainRecyclerAdapter(Context context, List<FragmentMainInfo> list) {
        this.context = context;
        this.list = list;
    }
    public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
        this.listener = listener;
    }

    public void setData(List<FragmentMainInfo> data) {
        if (this.list!=null){
            this.list.addAll(data);
        }else {
            this.list = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public ViewHocker onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_fragment_main, parent, false);
        view.setOnClickListener(this);
        return new ViewHocker(view);
    }

    @Override
    public void onBindViewHolder(final ViewHocker holder, int position) {
        FragmentMainInfo bean = list.get(position);
        if (bean != null) {
            ImageLoaderUtil.getInstance().loadImage("",holder.usericon);
            holder.username.setText(bean.getUser().getName());
            holder.time.setText(bean.getUpdatetime());

            holder.title.setText(bean.getTitle());
            holder.content.setText(Html.fromHtml(bean.getContent(),new HTMLImageGetter(context,holder.content),null));
            holder.desc.setText("浏览"+bean.getWatch()+"---评论"+bean.getComment());
            holder.like.setImageResource(R.mipmap.ic_star_empty);

            holder.like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (SystemLoginInfo.getboolean(SystemLoginInfo.LOGIN)) {
                        holder.like.setImageResource(R.mipmap.ic_star_full);
                    }else {
                        context.startActivity(new Intent(context, LoginActivity.class));
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list ==null?0:list.size();
    }

    @Override
    public void onClick(View v) {
        if (recyclerView != null && listener != null) {
            int position = recyclerView.getChildAdapterPosition(v);
            listener.OnRecyclerItemClick(recyclerView, v, position, list.get(position));
        }
    }

    public class ViewHocker extends RecyclerView.ViewHolder {
        private ImageView usericon;
        private TextView username;
        private TextView time;

        private TextView title;
        private TextView content;
        private TextView desc;

        private ImageView like;

        public ViewHocker(View itemView) {
            super(itemView);

            usericon = itemView.findViewById(R.id.id_item_fragment_main_author_icon);
            username = itemView.findViewById(R.id.id_item_fragment_main_author_name);
            time = itemView.findViewById(R.id.id_item_fragment_main_author_time);

            title = itemView.findViewById(R.id.id_item_fragment_main_title);
            content = itemView.findViewById(R.id.id_item_fragment_main_content);
            desc = itemView.findViewById(R.id.id_item_fragment_main_desc);

            like = itemView.findViewById(R.id.id_item_fragment_main_like);
        }
    }

    /**
     * 自定义RecyclerView的点击事件
     */
    public interface OnRecyclerItemClickListener {
        void OnRecyclerItemClick(RecyclerView parent, View view, int position, FragmentMainInfo data);
    }

}
